package com.jxs.android.weiget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jxs.android.ges.R;

public class BuLoadingDailog extends Dialog {

	private Context context;
	private LoadingListener loadingListener;
	private TextView title, message;
	private Button okBtn;
	private AnimationDrawable animationDrawable;

	public BuLoadingDailog(Context context) {
		super(context, R.style.IPone_Dialog_Bg);
		this.context = context;
		initContentView();
	}

	private void initContentView() {
		setContentView(R.layout.bu_loading_view);
		title = (TextView) findViewById(R.id.dialog_title);
		message = (TextView) findViewById(R.id.dialog_message);
		animationDrawable = (AnimationDrawable) findViewById(R.id.dialog_progress_bar).getBackground();
		okBtn = (Button) findViewById(R.id.ok);
		okBtn.setText(R.string.prompt_confirm);
		// setCancelable(false);
	}

	public void showLoading(int msgId) {// 隐藏按钮开始加载
		showLoading(context.getString(msgId));
	}

	public void showLoading(String msg) {// 隐藏按钮开始加载
		this.title.setText(null);
		okBtn.setVisibility(View.GONE);
		this.message.setText(msg);
		this.show();
	}

	public void showLoading(String title, String msg) {// 隐藏按钮开始加载
		this.title.setText(title);
		okBtn.setVisibility(View.GONE);
		this.message.setText(msg);
		this.show();
	}

	public void show() {
		animationDrawable.start();
		super.show();
	}

	@Override
	public void cancel() {
		animationDrawable.stop();
		super.cancel();
	}

	public void setLoadingListener(LoadingListener listener) {
		this.loadingListener = listener;
		if (null != loadingListener) {
			okBtn.setText(loadingListener.getOkText(context));
			okBtn.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					loadingListener.okLogic(BuLoadingDailog.this);
				}
			});
		}
	}

	static public interface LoadingListener {
		String getOkText(Context context);

		void okLogic(Dialog dialog);
	}
}
